package com.sp.fresh_produce.service.impl;

import com.sp.fresh_produce.common.Constant;
import com.sp.fresh_produce.controller.dto.VO.CartVO;
import com.sp.fresh_produce.ex.CustomException;
import com.sp.fresh_produce.ex.ExceptionCodeEnum;
import com.sp.fresh_produce.model.dao.CartMapper;
import com.sp.fresh_produce.model.dao.ProductMapper;
import com.sp.fresh_produce.model.pojo.Cart;
import com.sp.fresh_produce.model.pojo.Product;
import com.sp.fresh_produce.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CartMapper cartMapper;

    /**
     * 添加商品到购物车
     * @param userId
     * @param productId
     * @param count
     * @return
     */
    @Override
    public List<CartVO> add(Integer userId, Integer productId, Integer count){
        validProduct(productId, count);
        // 判断购物车是否存在该商品
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            // 不存在，新增记录
            cart = new Cart();
            cart.setUserId(userId);
            cart.setProductId(productId);
            cart.setQuantity(count);
            cart.setSelected(Constant.CartChecked.CHECKED);
            cartMapper.insertSelective(cart);
        } else {
            // 商品存在，购物车数据更新
            count = cart.getQuantity() + count;
            Cart cartNew = new Cart();
            cartNew.setQuantity(count);
            cartNew.setId(cart.getId());
            cartNew.setUserId(cart.getUserId());
            cartNew.setProductId(cart.getProductId());
            cartNew.setSelected(Constant.CartChecked.CHECKED);
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
        return this.list(userId);
    }

    /**
     * 验证商品信息
     * @param productId
     * @param count
     */
    private void validProduct(Integer productId, Integer count) {
        // 判断商品是否存在，商品是否上架
        // 获取商品信息
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product == null || product.getStatus().equals(Constant.ProductStatus.NOT_SALE)){
            throw new CustomException(ExceptionCodeEnum.PRODUCT_NOT_SALES.getCode(), ExceptionCodeEnum.PRODUCT_NOT_SALES.getMessage());
        }
        // 判断商品库存
        if (count > product.getStock()){
            throw new CustomException(ExceptionCodeEnum.PRODUCT_STOCK_ERROR.getCode(), ExceptionCodeEnum.PRODUCT_STOCK_ERROR.getMessage());
        }
    }

    /**
     * 购物车列表
     * @param userId
     * @return
     */
    @Override
    public List<CartVO> list(Integer userId){
        List<CartVO> cartVOList = cartMapper.selectCartByUserIdList(userId);
        for (int i = 0; i < cartVOList.size(); i++) {
            CartVO cartVO =  cartVOList.get(i);
            // 计算总价
            cartVO.setTotalPrice(cartVO.getPrice() * cartVO.getQuantity());
        }
        return cartVOList;
    }

    /**
     * 购物车更新
     * @param userId
     * @param productId
     * @param count
     * @return
     */
    @Override
    public List<CartVO> update(Integer userId, Integer productId, Integer count){
        validProduct(productId, count);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            // 不存在，抛出异常
            throw new CustomException(ExceptionCodeEnum.CART_NOT_EXIST.getCode(), ExceptionCodeEnum.CART_NOT_EXIST.getMessage());
        }
        // 更新
        Cart cartNew = new Cart();
        cartNew.setQuantity(count);
        cartNew.setId(cart.getId());
        cartNew.setProductId(cart.getProductId());
        cartNew.setUserId(cart.getUserId());
        cartNew.setSelected(Constant.CartChecked.CHECKED);
        cartMapper.updateByPrimaryKeySelective(cartNew);

        return this.list(userId);
    }

    /**
     * 购物车删除
     * @param userId
     * @param productId
     * @return
     */
    @Override
    public List<CartVO> delete(Integer userId, Integer productId){
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            // 不存在，无法删除
            throw new CustomException(ExceptionCodeEnum.DEL_ERROR.getCode(), ExceptionCodeEnum.DEL_ERROR.getMessage());
        }
        // 商品已存在在购物车，可以删除
        cartMapper.deleteByPrimaryKey(cart.getId());
        return this.list(userId);
    }

    /**
     * 购物车选中或者不选中
     */
    @Override
    public List<CartVO> selectOrNot(Integer userId, Integer productId, Integer selected){
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            // 不存在，抛出异常
            throw new CustomException(ExceptionCodeEnum.CART_NOT_EXIST.getCode(), ExceptionCodeEnum.CART_NOT_EXIST.getMessage());
        }else {
            cartMapper.selectOrNotSelect(userId, productId, selected);
        }
        return this.list(userId);
    }

    /**
     * 购物车全选中或者全不选中
     */
    @Override
    public List<CartVO> selectAllOrNot(Integer userId, Integer selected) {
        cartMapper.selectOrNotSelect(userId, null, selected);
        return this.list(userId);
    }
}
